# Author: Patrick Cunha Silva
# Project: Janusz, Cunha, and Junqueira
# Goal: Run RD models for Blacks (Main Analysis)
# First Version: October 27, 2022
# This Version: Jun 04, 2024

rm(list = ls())

# Read genFunctions
source(here("R-Files", "genFunctions.R"))

# Load data
load(file = here("Data", "blackRDD_final.RData"))


# Session ran on 06/04/2024
# > sessionInfo()
# R version 4.3.0 (2023-04-21)
# Platform: aarch64-apple-darwin22.4.0 (64-bit)
# Running under: macOS 14.5
# 
# Matrix products: default
# BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
# LAPACK: /opt/homebrew/Cellar/r/4.3.0_1/lib/R/lib/libRlapack.dylib;  LAPACK version 3.11.0
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# time zone: America/Chicago
# tzcode source: internal
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] stargazer_5.2.3 rdpower_2.2     rddensity_2.4   rdrobust_2.1.1  here_1.0.1     
# 
# loaded via a namespace (and not attached):
#   [1] vctrs_0.6.5      cli_3.6.1        rlang_1.1.1      generics_0.1.3   lpdensity_2.4    glue_1.6.2       colorspace_2.1-0 rprojroot_2.0.3 
# [9] scales_1.2.1     fansi_1.0.4      grid_4.3.0       munsell_0.5.0    tibble_3.2.1     MASS_7.3-58.4    lifecycle_1.0.3  compiler_4.3.0  
# [17] dplyr_1.1.2      pkgconfig_2.0.3  rstudioapi_0.14  R6_2.5.1         tidyselect_1.2.0 utf8_1.2.3       pillar_1.9.0     magrittr_2.0.3  
# [25] tools_4.3.0      gtable_0.3.3     ggplot2_3.4.2   


##############################################################
########### Part 1 - RD for Ratio, No Restrictions ###########
##############################################################

# Black Candidates (Ratio)
m1a <- rdrobust(y = blackRDD$totalBlacks,
                x = blackRDD$run_var_black,
                all = TRUE)
summary(m1a) # Positive, Null

# Black Candidates (Ratio)
m1b <- rdrobust(y = blackRDD$totalBlacks_ratio,
                x = blackRDD$run_var_black,
                all = TRUE)
summary(m1b) # Positive, Null


# Black Candidates (pct)
m1c <- rdrobust(y = blackRDD$totalBlacks_pct,
                x = blackRDD$run_var_black,
                all = TRUE)
summary(m1c) # Negative, Null


# Figures
pdf(here('plots_tables', 'BlackCandidates.pdf'), height = 8, width = 8)
genPlot(mydata = blackRDD, DV = 'totalBlacks', dvName = '# of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(0, 150), t1 = 145, t2 = 130)
genPlot(mydata = blackRDD, DV = 'totalBlacks_ratio', dvName = 'Ratio Afro-Brazilian Candidates Per Seat', run_var = 'run_var_black', ylim = c(2.5, 7.5), t1 = 7.5, t2 = 7)
genPlot(mydata = blackRDD, DV = 'totalBlacks_pct', dvName = '% of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(45, 90), t1 = 90, t2 = 86)
dev.off()

###########################################################
########### Part 2 - RD for Ratio, No Experience ##########
###########################################################

# Black Candidates
m2a  <- rdrobust(y = blackRDD$totalBlacks_noexp,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m2a) # Positive, Null

# Black Candidates (ratio)
m2b  <- rdrobust(y = blackRDD$totalBlacks_noexp_ratio,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m2b) # Positive, Null

# Black Candidates (pct)
m2c  <- rdrobust(y = blackRDD$totalBlacks_noexp_pct,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m2c) # Negative, Null

# Figures
pdf(here('plots_tables', 'BlackCandidates_NoExp.pdf'), height = 8, width = 8)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp', dvName = '# of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(0, 100), t1 = 100, t2 = 90)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp_ratio', dvName = 'Ratio Afro-Brazilian Candidates Per Seat', run_var = 'run_var_black', ylim = c(1.5, 4.5), t1 = 4.5, t2 = 4.2)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp_pct', dvName = '% of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(40, 80), t1 = 80, t2 = 76)
dev.off()

########################################################################
########### Part 3 - RD for Ratio, No Experience From Mayoral ##########
########################################################################

# Black Candidates
m3a  <- rdrobust(y = blackRDD$totalBlacks_noexp_frommp,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m3a) # Negative, Null

# Black Candidates (ratio)
m3b  <- rdrobust(y = blackRDD$totalBlacks_noexp_frommp_ratio,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m3b) # Negative, p<0.05

# Black Candidates (pct)
m3c  <- rdrobust(y = blackRDD$totalBlacks_noexp_frommp_pct,
                 x = blackRDD$run_var_black,
                 all = TRUE)
summary(m3c) # Negative, Null

# Figures
pdf(here('plots_tables', 'BlackCandidates_NoExp_Mayoral.pdf'), height = 8, width = 8)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp_frommp', dvName = '# of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(0, 10), t1 = 10, t2 = 9)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp_frommp_ratio', dvName = 'Ratio Afro-Brazilian Candidates Per Seat', run_var = 'run_var_black', ylim = c(0, 1), t1 = 1, t2 = 0.9)
genPlot(mydata = blackRDD, DV = 'totalBlacks_noexp_frommp_pct', dvName = '% of Afro-Brazilian Candidates', run_var = 'run_var_black', ylim = c(40, 100), t1 = 100, t2 = 94.5)
dev.off()


